import BaseScene, { SceneConfig } from '../../components/scenes/BaseScene';
import * as THREE from 'three';
import {
  AmbientLight,
  BoxGeometry,
  Color,
  DirectionalLight,
  HemisphereLight,
  Mesh,
  MeshBasicMaterial,
  MeshNormalMaterial,
  MeshPhongMaterial,
  PlaneGeometry,
  PointLight,
} from 'three';

class BasicScene extends BaseScene {
  constructor(config) {
    super(config);
    this.addPlane();
    this.addBox();
    this.addBox1();
    this.addBox2();
    this.addBox3();
    this.addLight();
    // this.addDirectionalLight();
  }

  private addPlane() {
    const { scene } = this;
    scene.background = new Color(0xaaaaaa);
    const planeGeometry = new PlaneGeometry(1000, 1000);
    const material = new MeshPhongMaterial();
    planeGeometry.rotateX(-Math.PI / 2);
    const boxMesh = new Mesh(planeGeometry, material);
    boxMesh.receiveShadow = true;
    scene.add(boxMesh);
  }

  private addBox() {
    const { scene } = this;
    const boxGeometry = new BoxGeometry(20, 20, 20);
    const material = new MeshBasicMaterial({ color: 0xff0 });
    const mesh = new Mesh(boxGeometry, material);
    mesh.position.setY(20);
    mesh.position.setX(-50);
    mesh.position.setZ(-50);
    mesh.castShadow = true; //default is false
    scene.add(mesh);
  }
  private addBox1() {
    const { scene } = this;
    const boxGeometry = new BoxGeometry(20, 20, 20);
    const material = new MeshBasicMaterial();
    const mesh = new Mesh(boxGeometry, material);
    mesh.position.setY(30);
    mesh.castShadow = true; //default is false
    mesh.rotateY(Math.PI / 4);
    scene.add(mesh);
  }

  private addBox2() {
    const { scene } = this;
    const boxGeometry = new BoxGeometry(20, 20, 20);
    const material = new MeshNormalMaterial();
    const mesh = new Mesh(boxGeometry, material);
    mesh.position.setY(30);
    mesh.position.setX(30);
    mesh.castShadow = true; //default is false
    mesh.rotateY(Math.PI / 4);
    scene.add(mesh);
  }
  private addBox3() {
    const { scene } = this;
    const boxGeometry = new BoxGeometry(20, 20, 20, 100, 100, 100);
    const material = new MeshPhongMaterial();
    const mesh = new Mesh(boxGeometry, material);
    mesh.position.setY(30);
    mesh.position.setX(60);
    mesh.castShadow = true; //default is false
    mesh.receiveShadow = false; //default
    mesh.rotateY(Math.PI / 4);
    scene.add(mesh);
  }
  private addLight() {
    const { scene, renderer } = this;
    const light = new PointLight(0xffffff, 0.7);
    light.castShadow = true;
    light.position.setX(100);
    light.position.setY(100);
    scene.add(light);
    scene.add(new THREE.PointLightHelper(light, 10));
    renderer['shadowMapEnabled'] = true;
  }
}

export const ShadowRender = (config: SceneConfig): BasicScene => {
  const renderWater = new BasicScene(config);
  renderWater.run();
  return renderWater;
};
